#include "qbrokenlinewidget.h"

#include "../../libs/shared/property/qabstractproperty.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qhostfactory.h"

#include <QPainter>
#include <QGradient>
#include <QPaintEngine>

QBrokenLineWidget::QBrokenLineWidget(QWidget *parent) :
    QAbstractGraphicsWidget(parent)
{
}

void QBrokenLineWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setPen(m_pen);
    p.setRenderHint(QPainter::Antialiasing,m_antialiasing);

    QPointFs ps=property("line_points").value<QPointFs>();

    QPoint* pt=new QPoint[ps.size()];

    int wid=this->width()-1-m_pen_info.m_width*2;
    int hei=this->height()-1-m_pen_info.m_width*2;

    int _x=m_pen_info.m_width;
    int _y=m_pen_info.m_width;

    for(int i=0;i<ps.size();i++)
    {
        QPointF pf=ps.at(i);
        pt[i].setX(_x+(int)(wid*pf.x()));
        pt[i].setY(_y+(int)(hei*pf.y()));
    }
    p.drawPolyline(pt,ps.size());

    delete [] pt;
}


QBrokenLineWidgetHost::QBrokenLineWidgetHost(QAbstractHost *parent):
    QAbstractGraphicsHost(parent)
{
}

void QBrokenLineWidgetHost::create_object()
{
    m_object=new QBrokenLineWidget;
    m_object->setObjectName("brokenLineWidget");
}

QString QBrokenLineWidgetHost::get_show_name()
{
    return tr("Broken Line");
}

QString QBrokenLineWidgetHost::get_show_group()
{
    return tr("Base Graphics");
}

QString QBrokenLineWidgetHost::get_show_icon()
{
    return ":/base_grahpics_images/broken_line.png";
}

void QBrokenLineWidgetHost::init_property()
{
    QAbstractGraphicsHost::init_property();

    QAbstractProperty* pro;
    pro=QPropertyFactory::create_property("PointFs");
    if(pro!=NULL)
    {
        pro->set_property("name","line_points");
        pro->set_attribute("show_name",tr("Points"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("line_points",pro->get_value());
        insert_property(pro);
    }

    set_property_value("geometry",QRect(0,0,100,100));
}

qRegisterHost("basegraphics.broken_line",QBrokenLineWidgetHost,5010)
